import { app, BrowserWindow, ipcRenderer, ipcMain, Menu, Tray } from 'electron'
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
let tray = null
let mainWindow
let childWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000,
    webPreferences: {
      nodeIntegration: true, //在网页中集成Node
      contextIsolation: false,
      enableRemoteModule: true, // 打开remote模块
    },

  })

  mainWindow.webContents.openDevTools(); //开启调试工具

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  const gotTheLock = app.requestSingleInstanceLock(); //禁止启动多次，只打开一个窗口
  if (!gotTheLock) {
    app.quit();
  }
}

// 子窗口
function childcrewindos() {
  childWindow = new BrowserWindow({
    width: 600,
    height: 400,
    useContentSize: true,
    resizable: false, //窗口宽高禁止拉动
    webPreferences: {
      nodeIntegration: true, //在网页中集成Node
      contextIsolation: false,
      enableRemoteModule: true, // 打开remote模块
    },
  });
  childWindow.on("closed", () => {
    childWindow = null;
  });
  childWindow.loadURL(winURL + `#/childList`);
  childWindow.webContents.openDevTools(); //开启调试工具
}



function initTrayIcon() {
  const iconPath = require('path').join(__static, '/icon-13.png');
  const tray = new Tray(iconPath);  //引入图标，添加右下角托盘的

  const trayContextMenu = Menu.buildFromTemplate([
    {
      label: '关于',
      click: () => {
      }
    },
    {
      label: '帮助',
      click: () => {
      }
    },
    {
      label: '列表页',
      click: () => {
        mainWindow.loadURL('http://localhost:9080/#/list')
      }
    },
    {
      label: '首页',
      click: () => {
        mainWindow.loadURL(winURL)
      }
    },
    {
      label: "显示/隐藏",
      click: () => {
        return mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
      }
    }, {
      label: '退出',
      click: () => {
        app.quit();
      }
    }
  ]);
  tray.setToolTip('fyh');  //设置名字
  // tray.on('click', () => {
  //   this.showAndFocusWindow();
  // });
  tray.setContextMenu(trayContextMenu) //设置这个图标的内容菜单

  Menu.setApplicationMenu(null) //自带File,Edit……,默认为英文 隐藏
  // tray.on('right-click', () => { 
  //   tray.popUpContextMenu(trayContextMenu);
  // });
}


app.on('ready', () => {
  initTrayIcon()
  createWindow()
  childcrewindos()
})


app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
